﻿using System;
using System.Collections;
using System.Web.UI;

namespace SPAdaptiveMenu.Controls
{
    ///<summary>
    ///</summary>
    public class MenuItemCollection : IHierarchicalEnumerable, IList
    {
        private readonly int _initialSize;
        private ArrayList _innerList;
        internal static MenuItemCollection Empty;

        ///<summary>
        ///</summary>
        public MenuItemCollection()
        {
            _initialSize = 10;
        }
        
        ///<summary>
        ///</summary>
        ///<param name="capacity"></param>
        public MenuItemCollection(int capacity)
        {
            _initialSize = 10;
            _initialSize = capacity;
        }

        ///<summary>
        ///</summary>
        ///<param name="value"></param>
        ///<exception cref="ArgumentNullException"></exception>
        public MenuItemCollection(MenuItem value)
        {
            _initialSize = 10;
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            _initialSize = 1;
            List.Add(value);
        }

        private ArrayList List
        {
            get { return _innerList ?? (_innerList = new ArrayList(_initialSize)); }
        }

        #region Implementation of IEnumerable

        public IEnumerator GetEnumerator()
        {
            return List.GetEnumerator();
        }

        public IHierarchyData GetHierarchyData(object enumeratedItem)
        {
            return (enumeratedItem as IHierarchyData);
        }

        #endregion

        #region Implementation of ICollection

        public void CopyTo(Array array, int index)
        {
            CopyToInternal(array, index);
        }

        internal virtual void CopyToInternal(Array array, int index)
        {
            List.CopyTo(array, index);
        }

        public int Count
        {
            get
            {
                return List.Count;
            }
        }

        public object SyncRoot
        {
            get
            {
                return List.SyncRoot;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return List.IsSynchronized;
            }
        }

        #endregion

        #region Implementation of IList

        ///<summary>
        /// Add MenuItem into collection
        ///</summary>
        ///<param name="value">MenuItem</param>
        ///<returns></returns>
        ///<exception cref="ArgumentNullException"></exception>
        public int Add(MenuItem value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            return List.Add(value);

        }

        public bool Contains(MenuItem value)
        {
            return List.Contains(value);
        }

        int IList.Add(object value)
        {
            OnValidate(value);
            return Add((MenuItem)value);
        }

        bool IList.Contains(object value)
        {
            OnValidate(value);
            return Contains((MenuItem)value);
        }
        void IList.Clear()
        {
            Clear();
        }

        protected virtual void OnValidate(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is MenuItem))
            {
                throw new ArgumentException("MenuItem");
            }
        }





        public void Clear()
        {
            List.Clear();
        }

        public int IndexOf(object value)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, object value)
        {
            throw new NotImplementedException();
        }

        public void Remove(object value)
        {
            throw new NotImplementedException();
        }

        public int IndexOf(MenuItem value)
        {
            return List.IndexOf(value);

        }

        public void Insert(int index, MenuItem value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            List.Insert(index, value);

        }

        public void Remove(MenuItem value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            List.Remove(value);
        }

        public void RemoveAt(int index)
        {
            List.RemoveAt(index);

        }

        object IList.this[int index]
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public MenuItem this[int index]
        {
            get
            {
                return (MenuItem)List[index];
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                List[index] = value;
            }

        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        #endregion
    }
}
